package view;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.crypto.spec.PSource.PSpecified;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeListener;

import org.apache.commons.math3.genetics.NPointCrossover;

import model.Computation;
import net.miginfocom.swing.MigLayout;

public class InputPanel extends JPanel {
    
    OutputPanel outputPanel;
	private JLabel rangeLabel;
    private JSpinner xLowerSpinner;
    private JLabel toLabel;
    private JSpinner xUpperSpinner;
    private JCheckBox singleCheck;
    private JLabel populationSizeLabel;
    private JSpinner popSizeSpinner;
    private JLabel sampleSizeLabel;
    private JSpinner sampleSizeSpinner;
    private JPanel updatePanel;
    private JButton updateButton;
    private JSlider populationSlide;
    private JSlider sampleSlide;
    private JPanel firstPanel;
    private JPanel secondPanel;    
    private JComboBox graphCombo;
    private JPanel topPanel;
    private JSlider popsizeSlider;
    
    public InputPanel() {
        setLayout(new MigLayout());
        setSize(1000, 200);
        initComponents();
        addComponents();
    }
    
    private void initComponents() {  
    	
    	topPanel = new JPanel();
    	topPanel.setLayout(new MigLayout());
    	
    	JComponent field; 
        // population range
        rangeLabel = new JLabel("Range: ");
        xLowerSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Computation.MAX_X, 1));
        field = ((JSpinner.DefaultEditor) xLowerSpinner.getEditor());
        Dimension prefSize = field.getPreferredSize();
        prefSize = new Dimension(30, prefSize.height);
        field.setPreferredSize(prefSize);
        
        toLabel = new JLabel("to: ");
        
        xUpperSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Computation.MAX_X, 1));
        field = ((JSpinner.DefaultEditor) xUpperSpinner.getEditor());
        prefSize = field.getPreferredSize();
        prefSize = new Dimension(30, prefSize.height);
        field.setPreferredSize(prefSize);
        
        graphCombo = new JComboBox();
        graphCombo.addItem("Bimodal");
        graphCombo.addItem("Normal");
        graphCombo.addItem("Random");
        graphCombo.addItem("Skewed Right");
        graphCombo.addItem("Skewed Left");
        graphCombo.addItem("Uniform");
        
        // population size
        populationSizeLabel = new JLabel("Population Size: ");
        
        popSizeSpinner = new JSpinner(new SpinnerNumberModel(0, 0, Computation.MAX_X, 1));
        field = ((JSpinner.DefaultEditor) popSizeSpinner.getEditor());
        prefSize = field.getPreferredSize();
        prefSize = new Dimension(30, prefSize.height);
        field.setPreferredSize(prefSize);
        
        // sample size
        sampleSizeLabel = new JLabel("Sample Size: ");
        
        sampleSizeSpinner = new JSpinner(new SpinnerNumberModel(1, 1, Computation.MAX_X, 1));
        field = ((JSpinner.DefaultEditor) sampleSizeSpinner.getEditor());
        prefSize = field.getPreferredSize();
        prefSize = new Dimension(30, prefSize.height);
        field.setPreferredSize(prefSize);
        
        updatePanel = new JPanel();
        updatePanel.setLayout(new MigLayout());
        updateButton = new JButton("Update Values");
        
        populationSlide = new JSlider(JSlider.HORIZONTAL, 0, 1000, 0);
       
        sampleSlide = new JSlider(JSlider.HORIZONTAL, 2, 10, 2);
        
        updatePanel.add(updateButton);
        updatePanel.add(populationSlide);
        updatePanel.add(sampleSlide);
        
        outputPanel = new OutputPanel();
        
        firstPanel = new JPanel();
        firstPanel.setLayout(new MigLayout());
        secondPanel = new JPanel();
        secondPanel.setLayout(new MigLayout());
    }
    
    private void addComponents() {
    	
    	topPanel.add(rangeLabel);
    	topPanel.add(xLowerSpinner);
    	topPanel.add(toLabel, "gapleft 10");
    	topPanel.add(xUpperSpinner, "wrap");
    	add(topPanel, "wrap");
    	addToFirstPanel();
    	add(firstPanel);
        addToSecondPanel();
        add(secondPanel, "gapleft 130, wrap");
        add(updatePanel, "wrap");
    
    }
    
    private void addToFirstPanel(){
    	
    	firstPanel.add(populationSizeLabel);
    	firstPanel.add(popSizeSpinner);
    	firstPanel.add(populationSlide, "wrap");
    	firstPanel.add(sampleSizeLabel);
    	firstPanel.add(sampleSizeSpinner);
    	firstPanel.add(sampleSlide, "wrap");
        firstPanel.add(graphCombo);
    }
    
    private void addToSecondPanel(){
    	
    	secondPanel.add(outputPanel);   	
    }
    
    public void updateData(Computation computation){
    	outputPanel.setpMeanField(computation.getPopulationMean());
    	outputPanel.setpVarianceField(computation.getPopulationVariance());
    	outputPanel.setsMeanField(computation.getSampleMean());
    	outputPanel.setsVarianceField(computation.getSampleVariance());
    }
    
    
    

    //--------------------------------
    // Getters and setters
    
    public int getXLower() {
        return (Integer) xLowerSpinner.getValue();
    }
    
    public int getXUpper() {
        return (Integer) xUpperSpinner.getValue();
    }
    
/*    public boolean isSingle() {
    	return singleCheck.isSelected();
    }*/
    
    public int getPopulationSize() {
        return (Integer) popSizeSpinner.getValue();
    }
    
    public int getSampleSize(){
    	return (Integer) sampleSizeSpinner.getValue();
    }
    
    public String getGraphType(){
    	return graphCombo.getSelectedItem().toString();
    }
    
    public void setXLower(int x) {
        xLowerSpinner.setValue(x);
    }
    
    public void setXUpper(int x) {
        xUpperSpinner.setValue(x);
    }
    
    public void setPopulationSize(int x) {
        popSizeSpinner.setValue(x);
    }
    
    public void setSampleSize(int x){
    	sampleSizeSpinner.setValue(x);
    }
    
    public void enableSingle() {
    	xUpperSpinner.setEnabled(false);
    	setXUpper(getXLower());
    }
    
    public void disableSingle() {
    	xUpperSpinner.setEnabled(true);
    }
    
/*    private ItemListener single() {
    	return new ItemListener() {
		@Override
		public void itemStateChanged(ItemEvent e) {
			if (singleCheck.isSelected()) 
				enableSingle();
			else
				disableSingle();
			
		}
		};
    }*/
    
    
    //--------------------------------
    // Listener setters
    
    public void addUpdateListener(ActionListener listener) {
        updateButton.addActionListener(listener);
    }
    
    public void addLowerSpinnerListener(ChangeListener listener) {
        xLowerSpinner.addChangeListener(listener);
    }
    
    public void addUpperSpinnerListener(ChangeListener listener) {
        xUpperSpinner.addChangeListener(listener);
    }
    
    public void addSingleListener(ItemListener listener) {
    	singleCheck.addItemListener(listener);
    }
    
    public void addNSpinnerListener(ChangeListener listener) {
        popSizeSpinner.addChangeListener(listener);
    }
    
    public void addnSpinnerListener(ChangeListener listener){
    	sampleSizeSpinner.addChangeListener(listener);
    }
    
    public void addPopSizeSliderListener(ChangeListener listener)
    {
    	populationSlide.addChangeListener(listener);
    }
    
    public void addSampleSizeSliderListener(ChangeListener listener)
    {
    	sampleSlide.addChangeListener(listener);
    }
}
